package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.YZMBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class RollOutTwoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.et_duihuan_num)
    EditText etDuihuanNum;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_enter)
    Button btEnter;
    private Intent intent;
    private String etAccount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rollouttwo);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.zhuanchu), R.mipmap.icon_close, getString(R.string.jilu));
        try{
            etAccount = getIntent().getStringExtra("etAccount");
            tvAccount.setText(etAccount);
        }catch (Exception e){

        }

    }

    @OnClick({R.id.et_duihuan_num, R.id.bt_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_duihuan_num:
                break;
            case R.id.bt_enter:
                if (TextUtils.isEmpty(etDuihuanNum.getText().toString().trim())){
                    showTips(getString(R.string.hintnum));
                }else   if (TextUtils.isEmpty(etPayPwd.getText().toString().trim())){
                    showTips(getString(R.string.paypwdhint));
                }else {
                    init();
                }
                break;
        }
    }
    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("account",  getIntent().getStringExtra("uuid"));
        parameters.put("number", etDuihuanNum.getText().toString().trim());
        parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()));
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign);
    }
    private void initdata(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.member_roll_out)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("account", getIntent().getStringExtra("uuid"))
                .addParams("number", etDuihuanNum.getText().toString().trim())
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        showTips(getString(R.string.error));
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG用户转账"+ response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                        }.getType());
                        int status = enterBean.getCode();
                        if (status == 1011) {
                            showTips(enterBean.getMsg());
                            setResult(RESULT_OK);
                            RollOutTwoActivity.this.finish();
                        } else if (status == 1004) {
                            intent = new Intent(getBaseContext(), LoginActivity.class);
                            startActivity(intent);
                        } else {
                            showTips(enterBean.getMsg());
                        }
                    }
                });
    }
    @Override
    public void RightNext() {
        super.RightNext();
        intent = new Intent(this, RecordActivity.class);
        intent.putExtra("type","3");
        intent.putExtra("classify","1");
        intent.putExtra("title", getString(R.string.zhuanchu));
        startActivity(intent);
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        RollOutTwoActivity.this.finish();
    }
}
